import axios from 'axios';

import store from '../vuex/store'
import { MessageBox } from 'element-ui'
import qs from 'qs'
import base from './config.js'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
//  request 请求拦截器 - 请求之前headers加token
axios.interceptors.request.use(
  config => {
    // 登录不用加token
    //  每次发送请求之前检测都vuex存有token,那么都要放在请求头发送给服务器
    if (config.url.indexOf('/tokens') < 0 && store.state.token) {
      config.headers.Authorization = `Bearer ${store.state.token}`;
    }
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
//  response 错误统一处理
axios.interceptors.response.use(
  res => {
    if (res.data.status === 0 && res.data.errorCode === 10003) {
      localStorage.removeItem('token');
      MessageBox.alert('需要重新登录', '提示', {
        type: 'warning',
        confirmButtonText: '重新登录'
      }).then(() => {
        window.location.href = '/login';
      });
      return Promise.reject(res);
    }
    return res;
  },
  err => {
    //  console.log(err);
    MessageBox.alert('服务器错误: ' + err, '提示', {
      type: 'error',
      closeOnClickModal: true,
      closeOnPressEscape: true
    }).then(() => {
    });
    return Promise.reject(err);
  }
);

//  export const requestLogin = params => { return axios.post(`${base}/login`, params).then(res => res.data); };

export const getUserList = params => {
  return axios.get(`${base}/user/list`, { params: params });
};

//  export const getUserListPage = params => { return axios.get(`${base}/user/listpage`, { params: params }); };

export const removeUser = id => {
  return axios.delete(`${base}/users/${id}`).then(res => res.data);
};

export const batchRemoveUser = params => {
  return axios.get(`${base}/user/batchremove`, { params: params });
};

//  export const editUser = params => { return axios.get(`${base}/user/edit`, { params: params }); };

//  export const addUser = params => { return axios.get(`${base}/user/add`, { params: params }); };

export const testReject = params => {
  return axios.get(`${base}/reject`, { params: params });
};

// 登录
export const requestLogin = params => {
  return axios.post(`${base}/tokens`, qs.stringify(params)).then(res => res.data);
};
/*
* 用户管理模块
*/

// 获取用户列表
export const getUsersList = params => {
  return axios.get(`${base}/users`, { params: params }).then(res => res.data);
};
// 用户详情
export const viewUser = id => {
  return axios.get(`${base}/users/${id}`).then(res => res.data);
};
// 新增用户
export const addUser = params => {
  return axios.post(`${base}/users`, qs.stringify(params)).then(res => res.data);
};
// 修改用户
export const editUser = params => {
  return axios.put(`${base}/users/${params.user_id}`, qs.stringify(params)).then(res => res.data);
};
// 新增用户验证手机号,邮箱是否被占用
export const checkUserPhoneEmail = params => {
  return axios.get(`${base}/users/check`, { params: params }).then(res => res.data);
};
// 新增用户头像url
export const uploadAvatarUrl = `${base}/files?category=avatar`;

/*
商品管理模块 start
 */

// 新增品牌
export const addBrand = params => {
  return axios.post(`${base}/brands`, qs.stringify(params)).then(res => res.data);
};
// 修改品牌
export const editBrand = params => {
  return axios.put(`${base}/brands/${params.brand_id}`, qs.stringify(params)).then(res => res.data);
};
// 删除品牌
export const deleteBrand = id => {
  return axios.delete(`${base}/brands/${id}`).then(res => res.data);
};
// 获取品牌列表
export const getBrandList = params => {
  return axios.get(`${base}/brands`, { params: params }).then(res => res.data);
};
// 新增品牌图片url
export const uploadBrandUrl = `${base}/files?category=brand`;
// 商品分类列表
export const getCategories = params => {
  return axios.get(`${base}/categories`, { params: params }).then(res => res.data);
};
// 获取分类选项列表
export const getCategoriesList = params => {
  return axios.get(`${base}/categories`, { params: params }).then(res => res.data);
};
// 编辑分类选项
export const editCategories = params => {
  return axios.put(`${base}/categories/${params.category_id}`, qs.stringify(params)).then(res => res.data);
};
// 新增分类选项值
export const addCategories = params => {
  return axios.post(`${base}/categories`, qs.stringify(params)).then(res => res.data);
};
// 新增分类图片url
export const uploadClassifyImage = `${base}/files?category=category`;
// 删除分类选项
export const deleteCategories = id => {
  return axios.delete(`${base}/categories/${id}`).then(res => res.data);
};
// 商品规格列表
export const getProductOptions = params => {
  return axios.get(`${base}/product-options`, { params: params }).then(res => res.data);
};
// 规格添加
export const addProductOptions = params => {
  return axios.post(`${base}/product-options`, qs.stringify(params)).then(res => res.data);
};
// 编辑规格
export const editProductOptions = params => {
  return axios.put(`${base}/product-options/${params.option_id}`, qs.stringify(params)).then(res => res.data);
};
// 删除规格
export const deleteProductOption = optionId => {
  return axios.delete(`${base}/product-options/${optionId}`).then(res => res.data)
};
// 批量删除规格
export const manyDeleteOptions = params => {
  // return axios.delete(`${base}/product-params/${paramId}`).then(res => res.data);
  return axios.post(`${base}/product-options`, qs.stringify(params)).then(res => res.data);
};
// 规格名称验证
export const checkOptionName = params => {
  return axios.get(`${base}/product-options/check`, { params: params }).then(res => res.data);
};
// 商品参数列表
export const getProductParams = params => {
  return axios.get(`${base}/product-params`, { params: params }).then(res => res.data)
};
// 参数添加
export const addProductParams = params => {
  return axios.post(`${base}/product-params`, qs.stringify(params)).then(res => res.data);
};
// 编辑参数
export const editProductParams = params => {
  return axios.put(`${base}/product-params/${params.param_id}`, qs.stringify(params)).then(res => res.data);
};
// 删除参数
export const deleteProductParams = paramId => {
  return axios.delete(`${base}/product-params/${paramId}`).then(res => res.data);
};
// 批量删除参数
export const manyDeleteParams = params => {
  return axios.post(`${base}/product-params`, qs.stringify(params)).then(res => res.data);
};
// 参数名称验证
export const checkParamName = params => {
  return axios.get(`${base}/product-params/check`, { params: params }).then(res => res.data);
};

// 卡片列表
export const getCardList = params => {
  return axios.get(`${base}/cards`, { params: params }).then(res => res.data);
};
// 卡片添加
export const addCard = params => {
  return axios.post(`${base}/cards`, qs.stringify(params)).then(res => res.data);
};
// 卡片编辑
export const editCard = params => {
  return axios.put(`${base}/cards/${params.card_id}`, qs.stringify(params)).then(res => res.data);
};
// 卡片删除
export const deleteCard = id => {
  return axios.delete(`${base}/cards/${id}`).then(res => res.data);
};
// 商品列表
export const getProductList = params => {
  return axios.get(`${base}/products`, { params: params }).then(res => res.data);
};
// 商品详情
export const getProduct = id => {
  return axios.get(`${base}/products/${id}`).then(res => res.data);
}
// 商品图片上传url
export const uploadProductImage = `${base}/files?category=product`;
// 新增商品
export const addProduct = params => {
  return axios.post(`${base}/products`, qs.stringify(params)).then(res => res.data);
}
// 修改商品信息
export const editProduct = params => {
  return axios.put(`${base}/products/${params.product_id}`, qs.stringify(params)).then(res => res.data);
}
// 商品详情上传图片
export const uploadDescImage = params => {
  return axios.post(`${base}/files?category=product`, params).then(res => res.data);
};
// 评论列表
export const getCommentList = params => {
  return axios.get(`${base}/comments`, { params: params }).then(res => res.data);
};
// 评论编辑
export const editComment = params => {
  return axios.put(`${base}/comments/${params.comment_id}`, qs.stringify(params)).then(res => res.data);
};
// 更改评论状态
export const editCommentStatus = params => {
  return axios.post(`${base}/comments`, qs.stringify(params)).then(res => res.data);
};
// 查看评论详情
export const getCommentDetail = params => {
  return axios.get(`${base}/comments/${params.comment_id}`, { params: params }).then(res => res.data);
};
// 新增运费模板名称
export const addFreightName = params => {
  return axios.post(`${base}/freight-tems`, qs.stringify(params)).then(res => res.data);
};
// 删除运费模板
export const delFreightItem = id => {
  return axios.delete(`${base}/freight-tems/${id}`).then(res => res.data);
};
// 获取运费模板列表
export const getFreightList = params => {
  return axios.get(`${base}/freight-tems`, { params: params }).then(res => res.data);
};
// 新增运费规则
export const addFreightRule = params => {
  return axios.post(`${base}/freight-rules`, qs.stringify(params)).then(res => res.data);
};
// 修改运费规则
export const editFreightRule = params => {
  return axios.put(`${base}/freight-rules/${params.rule_id}`, qs.stringify(params)).then(res => res.data);
};
// 删除运费规则
export const delFreightRule = id => {
  return axios.delete(`${base}/freight-rules/${id}`).then(res => res.data);
};
// 获取省市区（县）
export const getAreas = params => {
  return axios.get(`${base}/areas`, { params: params }).then(res => res.data);
};
// 获取国家列表
export const getCountries = params => {
  return axios.get(`${base}/countries`, { params: params }).then(res => res.data);
};
// 编辑国家列表
export const editCountries = params => {
  return axios.put(`${base}/countries/${params.country_id}`, qs.stringify(params)).then(res => res.data);
};
// 新增国家图片url
export const uploadCountriesImage = `${base}/files?category=country`;
// 快递公司列表
export const getExpressCompanyList = params => {
  return axios.get(`${base}/delivery-companies`, { params: params }).then(res => res.data);
};
// 快递公司添加
export const addExpressCompany = params => {
  return axios.post(`${base}/delivery-companies`, qs.stringify(params)).then(res => res.data);
};
// 快递公司编辑
export const editExpressCompany = params => {
  return axios.put(`${base}/delivery-companies/${params.delivery_id}`, qs.stringify(params)).then(res => res.data);
};
// 快递公司删除
export const deleteExpressCompany = id => {
  return axios.delete(`${base}/delivery-companies/${id}`).then(res => res.data);
};

/*
商品管理模块 end
 */

/*
系统设置模块 start
 */
// 获取参数
export const getSettings = params => {
  return axios.get(`${base}/settings`, { params: params }).then(res => res.data)
};
// 获取图片
export const uploadFilesUrl = `${base}/files?category=setting`;
// 修改参数
export const editSettings = params => {
  return axios.put(`${base}/settings/1`, qs.stringify(params)).then(res => res.data)
};
//  获取支付列表
export const getPayments = params => {
  return axios.get(`${base}/payments`, { params: params }).then(res => res.data)
}
//  添加支付方式
export const addPayment = params => {
  return axios.post(`${base}/payments`, qs.stringify(params)).then(res => res.data)
}
//   修改支付状态
export const editPayment = params => {
  return axios.put(`${base}/payments/${params.payment_id}`, qs.stringify(params)).then(res => res.data)
}
// 删除支付方式
export const deletePayment = id => {
  return axios.delete(`${base}/payments/${id}`).then(res => res.data)
};
// 获取国家列表
export const getCountryList = params => {
  return axios.get(`${base}/countries`, { params: params }).then(res => res.data)
}
// 获取缓存列表
export const getCacheList = params => {
  return axios.get(`${base}/caches`, { params: params }).then(res => res.data)
}
// 清除缓存
export const deleteCaches = params => {
  return axios.post(`${base}/caches`, qs.stringify(params)).then(res => res.data)
}
/*
系统设置模块 end
 */

/*
管理员模块start
 */
// admin列表
export const getAdminList = params => {
  return axios.get(`${base}/admins`, {params: params}).then(res => res.data)
}
// admin新增
export const addAdmin = params => {
  return axios.post(`${base}/admins`, qs.stringify(params)).then(res => res.data)
}
// admin修改
export const editAdmin = params => {
  return axios.put(`${base}/admins/${params.admin_id}`, qs.stringify(params)).then(res => res.data)
}
// 检验admin_name是否存在
export const checkAdminName = value => {
  return axios.get(`${base}/admins/check?value=${value}`).then(res => res.data);
}
// admin操作日志
export const getActionLogList = params => {
  return axios.get(`${base}/action-logs`, { params: params }).then(res => res.data);
};
// admin登陆日志
export const getLoginLogList = params => {
  return axios.get(`${base}/login-logs`, { params: params }).then(res => res.data);
};
// 权限组列表
export const getRoles = params => {
  return axios.get(`${base}/roles`, {params: params}).then(res => res.data);
};
// 检验自动回复规则是否存在
export const checkRoleName = value => {
  return axios.get(`${base}/roles/check?value=${value}`).then(res => res.data);
}
// 新增权限组
export const addRole = params => {
  return axios.post(`${base}/roles`, qs.stringify(params)).then(res => res.data)
}
// 修改权限组
export const editRole = params => {
  return axios.put(`${base}/roles`, qs.stringify(params)).then(res => res.data)
}

/*
管理员模块模块end
 */

/*
订单管理模块start
 */

// 获取订单列表
export const getOrderList = params => {
  return axios.get(`${base}/orders`, { params: params }).then(res => res.data);
};

// 订单详情
export const getOrder = id => {
  return axios.get(`${base}/orders/${id}`).then(res => res.data);
};

// 删除订单
export const deleteOrder = id => {
  return axios.delete(`${base}/orders/${id}`).then(res => res.data);
};
//  修改运费 & 修改收货人信息
export const editDeliveryInfo = params => {
  return axios.put(`${base}/orders/${params.order_id}`, qs.stringify(params)).then(res => res.data);
}

//  修改发票信息
export const editOrderInvoice = params => {
  return axios.put(`${base}/order-invoices/${params.order_invoice_id}`, qs.stringify(params)).then(res => res.data);
}

// 添加备注信息
export const addOrderHistory = params => {
  return axios.post(`${base}/order-histories`, qs.stringify(params)).then(res => res.data);
};

// 获取退货退款列表
export const getRefundList = params => {
  return axios.get(`${base}/refunds`, { params: params }).then(res => res.data);
};
// 获取退货退款列表
export const getRefundDetail = id => {
  return axios.get(`${base}/refunds/${id}`).then(res => res.data);
};
// 修改退货退款状态
export const editRefundStatus = params => {
  return axios.put(`${base}/refunds/${params.id}`, qs.stringify(params)).then(res => res.data)
}
/*
订单管理模块end
 */

/*
微信管理模块start
 */
// 获取规格列表
export const getWeChatMessages = params => {
  return axios.get(`${base}/wechat-messages`, { params: params }).then(res => res.data);
}
// 检验自动回复规则是否存在
export const checkRule = value => {
  return axios.get(`${base}/wechat-messages/check?value=${value}`).then(res => res.data);
}
// 新增自动回复规格
export const addWeChatMessage = params => {
  return axios.post(`${base}/wechat-messages`, qs.stringify(params)).then(res => res.data)
}
// 修改状态
export const editWeChatMessageStatus = id => {
  return axios.put(`${base}/wechat-messages/${id}`, qs.stringify({})).then(res => res.data)
}
// 删除自动回复规格
export const deleteWeChatMessage = id => {
  return axios.delete(`${base}/wechat-messages/${id}`).then(res => res.data)
}
// 获取菜单列表
export const getWeChatMenuList = params => {
  return axios.get(`${base}/wechat-menus`, { params: params }).then(res => res.data);
}
// 新增菜单
export const addWeChatMenu = params => {
  return axios.post(`${base}/wechat-menus`, qs.stringify(params)).then(res => res.data)
}
// 编辑菜单
export const editWeChatMenu = params => {
  return axios.put(`${base}/wechat-menus/${params.menu_id}`, qs.stringify(params)).then(res => res.data)
}
// 删除菜单
export const deleteMenu = id => {
  return axios.delete(`${base}/wechat-menus/${id}`, qs.stringify({})).then(res => res.data)
}
// 素材上传url
export const uploadMedia = `${base}/media`;

// 素材列表
export const getWeChatMedia = params => {
  return axios.get(`${base}/media`, {params: params}).then(res => res.data)
}

/*
微信管理模块end
 */
/*
 优惠劵模块 start
 */
//  添加优惠劵
export const addCoupon = params => {
  return axios.post(`${base}/coupons`, qs.stringify(params)).then(res => res.data)
};
//  修改优惠劵
export const editCoupon = params => {
  return axios.put(`${base}/coupons/${params.coupon_id}`, qs.stringify(params)).then(res => res.data)
};
// 删除优惠劵
export const deleteCoupon = id => {
  return axios.delete(`${base}/coupons/${id}`).then(res => res.data)
};
// 获取优惠劵列表
export const getCouponList = params => {
  return axios.get(`${base}/coupons`, { params: params }).then(res => res.data)
};
// 获取优惠劵
export const getCoupon = id => {
  return axios.get(`${base}/coupons/${id}`).then(res => res.data);
};
/*
优惠劵模块end
 */
// 微信广告列表
export const getAdvertList = params => {
  return axios.get(`${base}/advertisements`, {params: params }).then(res => res.data)
}
// 广告图片上传
export const uploadAdvertImg = `${base}/files?category=advertisement`;
// 新增广告
export const addAdvert = params => {
    return axios.post(`${base}/advertisements`, qs.stringify(params)).then(res => res.data)
};
// 修改广告
export const editAdvert = params => {
    return axios.put(`${base}/advertisements/${params.ad_id}`, qs.stringify(params)).then(res => res.data)
};
// 广告详情
export const getAdvert = id => {
    return axios.get(`${base}/advertisements/${id}`).then(res => res.data)
}
// 删除广告
export const deleteAdvert = id => {
    return axios.delete(`${base}/advertisements/${id}`).then(res => res.data)
};
// 广告批量删除
export const manyDeleteadvert = params => {
    return axios.post(`${base}/advertisements`, qs.stringify(params)).then(res => res.data);
};

